<template>
  <div>
    <el-dialog :title="ruleForm.id == '' ? '添加告警模板' : '编辑告警模板'" :visible.sync="addEditIsDialog_c" @close="resetForm('formName')" width="800px">
      <el-form :model="ruleForm" :rules="rules" ref="formName" label-width="120px" class="demo-ruleForm" :class="{ 'form-rules-show': seeType }">
        <el-row>
          <el-col :span="24">
            <el-form-item label="公司名称" prop="companyId">
              <el-select v-model="ruleForm.companyId" placeholder="请选择公司名称" clearable :disabled="seeType || userType == 2">
                <el-option v-for="item in companyArray" :key="item.id" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板名称：" prop="name">
              <el-input v-model="ruleForm.name" placeholder="请输入模板名称" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div v-for="(item, index) in ruleForm.modelAlarmDetailList" :key="index">
          <el-row>
            <el-col :span="12">
              <el-form-item label="告警类型：" prop="alarmType">
                <el-input v-model="item.alarmType" placeholder="请输入告警类型" clearable maxlength="50" show-word-limit :disabled="seeType"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="告警颜色：" prop="colorType">
                <el-color-picker v-model="item.colorType" :disabled="seeType"></el-color-picker>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <span @click="handleDel(index)" style="
                        color: #f56c6c;
                        cursor: pointer;
                        margin-top: 15px;
                        margin-left: 10px;
                        display: block;
                      ">删除</span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="推送方式：" prop="pushMode"><el-select multiple v-model="item.pushMode" placeholder="请选择推送方式" clearable>
                  <el-option label="邮件" value="0"></el-option>
                  <el-option label="短信" value="1"></el-option>
                  <el-option label="语音" value="2"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="开始值：" prop="startValue">
                <el-input-number v-model="item.startValue" placeholder="请输入开始值" :precision="2" :step="1" :max="9999999" :min="0" :disabled="seeType"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="结束值：" prop="endValue">
                <el-input-number v-model="item.endValue" placeholder="请输入结束值" :precision="2" :step="1" :max="9999999" :min="0" :disabled="seeType"></el-input-number>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="">
              <span @click="handleAdd" style="color: #256bff; cursor: pointer">+添加告警类型</span>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('formName')">取 消</el-button>
        <el-button type="primary" @click="submitForm('formName')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script type="text/ecmascript-6">
import { getModelAlarmSettingsDetail, saveModelAlarmSettingsDetail } from '@/api/alarm'
// import { getCompanyInfos } from "@/api/company";
import { getCompanyInfoTree } from '@/api/company'

export default {
  props: {
    addEditId: {
      type: String,
      default: ''
    },
    addEditStatus: {
      type: String,
      default: 'add'
    },
    addEditIsDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        // 表单字段
        id: '',
        companyId: '',
        name: '',
        modelAlarmDetailList: [],
        pushMode: []
      },
      rules: {
        // 表单规则
        companyId: [{ required: true, message: '请选择公司名称', trigger: 'change' }],
        name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }]
      },
      addEditIsDialog_c: false, // 弹窗是否显示
      seeType: false, // 是否禁用
      companyArray: [], // 机构数组
      userType: '', // 账号类型（0超级管理员，2组织管理员，3一般管理员）
      companyId: '' // 机构id
    }
  },
  watch: {
    addEditIsDialog(val) {
      this.addEditIsDialog_c = val

      if (this.addEditIsDialog_c) {
        // 添加
        if (this.addEditStatus == 'add') {
          this.ruleForm.id = ''
          this.seeType = false
        }

        // 编辑
        if (this.addEditStatus == 'edit') {
          this.ruleForm.id = this.addEditId
          this.seeType = false
          this.getDetail()
        }

        // 查看
        if (this.addEditStatus == 'see') {
          this.ruleForm.id = this.addEditId
          this.seeType = true
          this.getDetail()
        }

        // 获取机构列表
        this.getCompanyList()
        // 获取公司列表
        //  this.getCompanyInfoTree();
      }
    }
  },
  methods: {
    // 详情
    getDetail() {
      getModelAlarmSettingsDetail({ id: this.addEditId }).then((res) => {
        if (res.code == 200) {
          res.data.modelAlarmDetailList.forEach((val) => {
            if (val.pushMode) {
              val.pushMode = val.pushMode.split(',')
            }
            if (val.startValue == null) {
              val.startValue = undefined
            }
            if (val.endValue == null) {
              val.endValue = undefined
            }
          })

          this.ruleForm = res.data
        }
      })
    },
    // 提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.ruleForm.modelAlarmDetailList.length == 0) {
            this.$message({
              type: 'error',
              message: '至少添加一条告警类型'
            })
            return
          }

          let flag = true
          let _this = this
          this.ruleForm.modelAlarmDetailList.every(function (val) {
            if (val.alarmType === '' || val.alarmType == undefined) {
              _this.$message({
                type: 'error',
                message: '请输入告警类型'
              })
              flag = false
              return false
            }

            if (val.colorType == '' || val.colorType == undefined) {
              _this.$message({
                type: 'error',
                message: '请选择告警颜色'
              })
              flag = false
              return false
            }

            if ((val.startValue === '' || val.startValue == undefined) && (val.endValue === '' || val.endValue == undefined)) {
              _this.$message({
                type: 'error',
                message: '最大值、最小值至少输入一个'
              })
              flag = false
              return false
            }

            if (val.startValue !== '' && val.startValue != undefined && val.endValue !== '' && val.endValue != undefined && val.startValue > val.endValue) {
              _this.$message({
                type: 'error',
                message: '开始值不能大于结束值'
              })
              flag = false
              return false
            }

            return true
          })

          if (!flag) {
            return
          }
          this.ruleForm.modelAlarmDetailList.map((item) => {
            item.pushMode = item.pushMode.join(',')
          })
          saveModelAlarmSettingsDetail(this.ruleForm).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: res.msg
              })

              // 关闭弹窗
              this.$parent.closeAddEdit()
              // 刷新列表
              this.$parent.onSubmit()
            }
          })
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.ruleForm.modelAlarmDetailList = []
      this.$refs[formName].resetFields()
      this.$parent.closeAddEdit()
    },
    // 获取机构列表
    getCompanyList() {
      getCompanyInfoTree().then((res) => {
        if (res.code == 200) {
          this.companyArray = res.data
        }
      })
    },

    // 添加告警类型
    handleAdd() {
      this.ruleForm.modelAlarmDetailList.push({
        id: '',
        modelAlarmSettingsId: '',
        alarmType: '',
        startValue: undefined,
        endValue: undefined,
        colorType: ''
      })
    },
    // 删除告警类型
    handleDel(index) {
      this.ruleForm.modelAlarmDetailList.splice(index, 1)
    }
  }
}
</script>

<style lang="less" scoped>
.demo-ruleForm {
  width: 95%;
  margin: 0 auto;
  padding-top: 10px;

  .el-input-number {
    width: 100%;
  }
}
</style>
